using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace StartMenuSearcher
{
	internal class Shortcut : ShellShortcut, IComparable<Shortcut>
	{
		private string _name;

		public Shortcut(string path)
			: base(path)
		{
			_name = ExtractName(path);			
		}

		private string ExtractName(string path)
		{
			int startIndex = path.LastIndexOf("\\") + 1;
			int stopIndex = path.LastIndexOf(".lnk");
			if (stopIndex == -1)
			{
				stopIndex = path.Length - 1;
			}

			return path.Substring(startIndex, stopIndex - startIndex);
		}

		public string Name
		{
			get { return _name; }
		}

		public void Run()
		{
			Process.Start(base.ShortcutPath);
		}

		/// <summary>
		/// Returns true if the shortcut's name contains a 
		/// word starting with the filter text.
		/// </summary>
		/// <remarks>This method is case in-sensitive.</remarks>
		public bool Matches(string filter)
		{
			if (_name.StartsWith(filter, StringComparison.InvariantCultureIgnoreCase))
				return true;

			if (_name.IndexOf(" " + filter, StringComparison.InvariantCultureIgnoreCase) != -1)
				return true;

			return false;
		}

		public override string ToString()
		{
			return _name;
		}

		public int CompareTo(Shortcut other)
		{
			if (_name.Length != other._name.Length)
			{
				return _name.Length.CompareTo(other._name.Length);
			}
			else
			{
				return _name.CompareTo(other._name);
			}
		}
	}
}
